<script setup lang="ts">
import { useSysFile } from "./hook";
import { getPickerShortcuts } from "@/utils/utils";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import ReFileUpload from "@/components/ReFileUpload";
import Delete from "@iconify-icons/ep/delete";
import Add from "@iconify-icons/ep/plus";
import Refresh from "@iconify-icons/ep/refresh";
import { PureTableBar } from "@/components/RePureTableBar";
import { message } from "@/utils/message";

defineOptions({
  name: "systemFiles"
});
const formRef = ref();
const tableRef = ref();
const files = ref([]);
const {
  form,
  loading,
  columns,
  dataList,
  resetForm,
  pagination,
  selectedNum,
  onSearch,
  onbatchDel,
  onPreviewPdf,
  onPreviewAudio,
  onPreviewVideo,
  handleSizeChange,
  onSelectionCancel,
  handleSelectionChange,
  handleCurrentChange
} = useSysFile(tableRef);
const onSuccess = (res: any) => {
  message(res.message, { type: "success" });
  files.value = [];
  onSearch();
};
</script>

<template>
  <div class="main">
    <el-form
      ref="formRef"
      inline
      :model="form"
      class="bg-bg_color pl-8 pt-[12px] overflow-auto"
    >
      <el-form-item label="文件名称">
        <el-input v-model="form.name" placeholder="文件名称" clearable />
      </el-form-item>
      <el-form-item label="文件类型" class="!w-[260px]">
        <el-select v-model="form.ext" placeholder="文件类型" clearable>
          <el-option label="全部" value="" />
          <el-option label="图片" value="image" />
        </el-select>
      </el-form-item>
      <el-form-item label="上传用户">
        <el-input
          v-model="form.userName"
          placeholder="上传用户账号"
          clearable
        />
      </el-form-item>
      <el-form-item label="上传时间">
        <el-date-picker
          v-model="form.createdAt"
          :shortcuts="getPickerShortcuts()"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始上传日期时间"
          end-placeholder="结束上传日期时间"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :loading="loading"
          :icon="useRenderIcon('ri:search-line')"
          @click="onSearch"
          >{{ $t("buttons.pureSearch") }}</el-button
        >
        <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
          {{ $t("buttons.pureReset") }}
        </el-button>
      </el-form-item>
    </el-form>
    <PureTableBar title="文件列表" :columns="columns" @refresh="onSearch">
      <template #buttons>
        <ReFileUpload
          v-model="files"
          :showList="false"
          list-type="text"
          :tip="false"
          :size="1"
          :success="onSuccess"
        >
          <el-button type="primary" plain>上传附件</el-button>
        </ReFileUpload>
      </template>
      <template v-slot="{ size, dynamicColumns }">
        <div
          v-if="selectedNum > 0"
          v-motion-fade
          class="bg-[var(--el-fill-color-light)] w-full h-[46px] mb-2 pl-4 flex items-center"
        >
          <div class="flex-auto">
            <span
              style="font-size: var(--el-font-size-base)"
              class="text-[rgba(42,46,54,0.5)] dark:text-[rgba(220,220,242,0.5)]"
            >
              已选 {{ selectedNum }} 项
            </span>
            <el-button type="primary" text @click="onSelectionCancel">
              取消选择
            </el-button>
          </div>
          <el-popconfirm title="是否确认删除?" @confirm="onbatchDel">
            <template #reference>
              <el-button type="danger" text class="mr-1"> 批量删除 </el-button>
            </template>
          </el-popconfirm>
        </div>
        <pure-table
          ref="tableRef"
          row-key="id"
          align-whole="center"
          table-layout="auto"
          :loading="loading"
          :size="size"
          :adaptiveConfig="{ offsetBottom: 108 }"
          :data="dataList"
          :columns="dynamicColumns"
          :pagination="{ ...pagination, size }"
          :header-cell-style="{
            background: 'var(--el-fill-color-light)',
            color: 'var(--el-text-color-primary)'
          }"
          @selection-change="handleSelectionChange"
          @page-size-change="handleSizeChange"
          @page-current-change="handleCurrentChange"
        >
          <template #preview="{ row }">
            <template v-if="['mp3', 'm4a'].includes(row.ext)">
              <el-button @click="onPreviewAudio(row)"> 播放 </el-button>
              <!-- <audio controls>
                <source :src="row.url" />
              </audio> -->
            </template>
            <template v-else-if="['mp4', 'avi', 'wmv'].includes(row.ext)">
              <!-- <video width="180" height="180" controls class="m-auto">
                <source :src="row.url" type="video/mp4" />
              </video> -->
              <el-button @click="onPreviewVideo(row)"> 播放 </el-button>
            </template>
            <template v-else-if="['pdf'].includes(row.ext)">
              <el-button @click="onPreviewPdf(row)"> 预览 </el-button>
            </template>
            <template
              v-else-if="
                ['png', 'jpg', 'gif', 'jpeg', 'jfif', 'svg'].includes(row.ext)
              "
            >
              <el-image
                preview-teleported
                loading="lazy"
                :src="row.url"
                :preview-src-list="[row.url]"
                fit="cover"
                class="w-[100px] h-[100px]"
              />
            </template>
          </template>
          <template #operation="{ row }">
            <el-popconfirm
              :title="`是否确认删除这条数据`"
              @confirm="onbatchDel(row)"
            >
              <template #reference>
                <el-button
                  class="reset-margin"
                  link
                  type="danger"
                  :size="size"
                  :icon="useRenderIcon(Delete)"
                >
                  {{ $t("buttons.pureDelete") }}
                </el-button>
              </template>
            </el-popconfirm>
          </template>
        </pure-table>
      </template>
    </PureTableBar>
  </div>
</template>

<style lang="scss" scoped></style>
